import java.util.*;
public class Main {
    public static void main(String[] args) {
        Student head = new Student();
        boolean flag = true;
        int i = 0;
        int ch;
        while (true) {
            System.out.println("Please input your choice:");
            Scanner in = new Scanner(System.in);
            System.out.println("1.Add a new student");
            System.out.println("2.Delete an existing student");
            System.out.println("3.Revise an existing student");
            System.out.println("4.Sort");
            System.out.println("5.Refer");
            System.out.println("0.Exit");
            ch = in.nextInt();
            switch (ch) {
                case 1:{
                    flag = Add(head,flag);
                    Print(head);
                    break;
                }
                case 2:{
                    Delete(head);
                    Print(head);
                    break;
                }
                case 3:{
                    Revise(head);
                    Print(head);
                    break;
                }
                case 4:{
                    head = Sort(head);
                    Print(head);
                    break;
                }
                case 5:{
                    Refer(head);
                    break;
                }
                case 0:{
                    System.exit(0);
                    break;
                }
            }
        }

    }
    static boolean Add(Student head,boolean flag){
        Student p = head;
        boolean f = flag;
        Scanner in = new Scanner(System.in);
        if (f) {
            f = false;
        }
        else {
            while (p.nextstu != null) p = p.nextstu;
            p.nextstu = new Student();
            p = p.nextstu;
        }
        System.out.println("Please input your name:");
        p.setName(in.next());
        System.out.println("Please input your ID number:");
        p.setNum(in.nextInt());
        System.out.println("Please input your score:");
        p.setScore(in.nextInt());
        return f;
    }
    static void Delete(Student head){
        Student p = head;
        Scanner in = new Scanner(System.in);
        System.out.println("Please input the ID of the student(Delete):");
        int id = in.nextInt();
        while (true) {
            if (p == null) {
                System.out.println("Not found!");
                return;
            }
            if (p.nextstu.getNum()!=id) p = p.nextstu;
            else break;
        }
        p.nextstu = p.nextstu.nextstu;
        return;
    }
    static void Revise(Student head){
        Student p = head;
        Scanner in = new Scanner(System.in);
        System.out.println("Please input the ID of the student(Revise):");
        int id = in.nextInt();
        while (true) {
            if (p==null) {
                System.out.println("Not found!");
                return;
            }
            if (p.getNum()!=id) p = p.nextstu;
            else break;
        }
        System.out.println("Please input your name(Revise):");
        p.setName(in.next());
        System.out.println("Please input your ID number(Revise):");
        p.setNum(in.nextInt());
        System.out.println("Please input your score(Revise):");
        p.setScore(in.nextInt());
        return;
    }
    static void Refer(Student head) {
        Student p = head;
        Scanner in = new Scanner(System.in);
        System.out.println("Please input the ID of the student(Revise):");
        int id = in.nextInt();
        while (true) {
            if (p==null) {
                System.out.println("Not found!");
                return;
            }
            if (p.getNum()!=id) p = p.nextstu;
            else break;
        }
        System.out.println("Name:"+p.getName() + " Num:" + p.getNum() + " Score:" + p.getScore());
        return;
    }
    static Student Sort(Student head) {
        Student p = head;
        Student p1 = p;
        Student p2;
        Student p3 = p;
        Student d = head;
        while (p3!=null) {
            while (p3 != null) {
                if (p1.nextstu.getScore() < p3.getScore()) {
                    p1 = p3;
                }
                p3 = p3.nextstu;
            }
        }
        return d;
    }
    static void Print(Student head){
        Student p = head;
        while (p!=null) {
            System.out.println("Name:"+p.getName() + " Num:" + p.getNum() + " Score:" + p.getScore());
            p = p.nextstu;
        }
    }
}
